/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.*;
import picturewolrd.PictureWolrd;
import picturewolrd.Screen;

/**
 * /**
 *
 * @author pt-user
 */
public class TableIcon extends JFrame implements Runnable {

    Thread t;

    public void run() {



        try {
            for (int i = 5; i > 0; i--) {
                System.out.println("Child Thread: " + i);
                // Let the thread sleep for a while.
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            System.out.println("Child interrupted.");
        }
        System.out.println("Exiting child thread.");
    }
    public Photo[] datapt;
public Object[][] data;
    public TableIcon() throws IOException {
        Image timage = Toolkit.getDefaultToolkit().getImage("c:/lena512.bmp");
        ImageIcon timageicon = new ImageIcon(timage);

        Photo p = new Photo("c:/lena512.bmp");
        p.createThumbnail();
        ImageIcon pIcon = new ImageIcon(p.getImgIcon());

        File aboutfile = new File("c:/lena512.bmp");
        Image about = null;

        about = ImageIO.read(aboutfile);
        int w = about.getWidth(null);
        ImageIcon aboutIcon = new ImageIcon(about);
        // BufferedImage i = null;
        //   i=(BufferedImage) getToolkit().getImage(getClass().getResource("c:/lena512.bmp"));
        ImageIcon addIcon;
        addIcon = new ImageIcon("c:/vietnam.jpg");
        ImageIcon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        datapt=new Photo[]{p};
        data = new Object[][]{
           
            {p.getThumbnail(), "timage"}, // {i, "Something"},
             {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
            {timageicon, "timage"},
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model) {
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            @Override
            public Class getColumnClass(int column) {

                return getValueAt(1, column).getClass();
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                //all cells false
                return false;
                //Only the third column
                //return column == 3;
            }
        };
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                   JTable target = (JTable) e.getSource();
                   int trow = target.getSelectedRow();
                   String urlselected=datapt[trow].getPhotoName();
                    //int tcolumn = target.getSelectedColumn();
                    picturewolrd.PictureWolrd p= new picturewolrd.PictureWolrd();
                   // Screen s = new Screen();
                   
                   // s.setPicurl("c:/vietnam.jpg");
                    Screen.setPicurl(urlselected);
                    Screen.setFirstrun(1);
                    try {
                        p.main(null);
                        // do some action
                    } catch (IOException ex) {
                        Logger.getLogger(TableIcon.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        // table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setRowHeight(90);

        table.getColumnModel().getColumn(0).setPreferredWidth(90);
        table.setMinimumSize(new Dimension(800, 800));
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);

    }

    public static void main(String[] args) throws IOException {
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        //   JTableDisplay();
        //  JTable table = new JTable();
        ///  JScrollPane spTable = new JScrollPane(table);
        // JPanel panel = new JPanel();
//
        //  panel.add(spTable);
        //  table.setVisible(true);
    }
}
